package com.loubii.sunshineSchool.ui.fragments;

import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.adapter.BillAdapter;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.pojo.UserItem;
import com.loubii.sunshineSchool.util.ToastUtil;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

public class FragmentFriend extends BaseEventFragment {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @BindView(R.id.tv_title_time)
    TextView mTvTitleTime;

    @BindView(R.id.ultimate_recycler_view)
    UltimateRecyclerView mUltimateRecyclerView;
    @BindView(R.id.app_bar)
    AppBarLayout mAppBar;

    private BillAdapter mBillAdapter;
    private List<UserItem> items;

    public FragmentFriend() {
        // Required empty public constructor
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bill;
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initView(View view) {
        initToolbar();
        initRecycleView();
        searchFriend();
    }

    private void searchFriend(){
        HttpBin.searchFriend(context, new HttpBin.ResponseListener<List<UserItem>>() {
            @Override
            public void onResponse(Call<List<UserItem>> call, Response<List<UserItem>> response) {
                mBillAdapter.setData(response.body());
                mBillAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<List<UserItem>> call, Throwable t) {

            }
        });
    }


    private void initRecycleView() {
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mUltimateRecyclerView.setLayoutManager(linearLayoutManager);
        mBillAdapter = new BillAdapter(context);

        //悬浮头部布局需要加入
        StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(mBillAdapter);
        mUltimateRecyclerView.addItemDecoration(headersDecor);
        //设置下拉刷新
        mUltimateRecyclerView.setDefaultSwipeToRefreshColorScheme(getResources().getColor(R.color.colorPrimary));
        mUltimateRecyclerView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        searchFriend();
                        ToastUtil.showShort("已更新");
                    }
                }, 1000);
            }
        });
        mUltimateRecyclerView.setAdapter(mBillAdapter);
        mUltimateRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //如果滑动到第一条且完全可见则展开appbarLayout
                    int visiblePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                    if (visiblePosition == 0) {
                        mAppBar.setExpanded(true);
                    }
                }
            }
        });
    }

    private void initToolbar() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
    }

}
